Skip to main content

868. Binary Gap

Question

Given a positive integer n, find and return the longest distance between any two adjacent 1's in the binary representation of n. If there are no two adjacent 1's, return 0.

Two 1's are adjacent if there are only 0's separating them (possibly no 0's). The distance between two 1's is the absolute difference between their bit positions. For example, the two 1's in "1001" have a distance of 3.

Example 1:

Input: n = 22
Output: 2
Explanation: 22 in binary is "10110".
The first adjacent pair of 1's is "10110" with a distance of 2.
The second adjacent pair of 1's is "10110" with a distance of 1.
The answer is the largest of these two distances, which is 2.
Note that "10110" is not a valid pair since there is a 1 separating the two 1's underlined.

Example 2:

Input: n = 8
Output: 0
Explanation: 8 in binary is "1000".
There are not any adjacent pairs of 1's in the binary representation of 8, so we return 0.

Example 3:

Input: n = 5
Output: 2
Explanation: 5 in binary is "101".

Constraints:

1 <= n <= 109


Approach

  1. First, convert the input n into bit. We can check each of the bits, by shifting it to the left until the end of n.
  2. Reverse the string of bits just now, and we will have the bit representation of n.
  3. Iterate through the bits, look for the first occurence of 1 and compare the distance between it and the next i.

Solution

class Solution {
public:
int binaryGap(int n) {
string bin;

while(n){
if(n & 1){
bin += "1";
}else{
bin += "0";
}
n >>= 1;
}

reverse(bin.begin(),bin.end());

int dist = 0;
int prev = -1;

for(int i = 0; i < bin.size(); i++){
if(bin[i] == '1'){
if (prev == -1) prev = i;
else{
dist = max(dist,i - prev);
prev = i;
}
}
}
return dist;
}
};